# -*- coding: utf-8 -*-
# File:add_member_page.py
# Time: 
# Author : xingtiantian
from time import sleep

import allure
import pytest
from faker import Faker
from selenium.webdriver.common.by import By
from qiyeweixin.base.basepage import BasePage
from qiyeweixin.utils.log import logger


class AddMemberPage(BasePage):
    """添加成员界面"""
    _NAME = (By.ID, 'username')  # 姓名
    _MID = (By.ID, 'memberAdd_acctid')  # 账号
    _PHONE = (By.ID, 'memberAdd_phone')  # 电话
    _SAVE_CONTINUE_BTN = (By.XPATH, '//div[contains(@class,"ww_operationBar")][1]//a[contains(@class,"js_btn_continue")]')  # 保存并继续添加 按钮
    _SAVE_BTN = (By.XPATH, '//div[contains(@class,"ww_operationBar")][1]//a[contains(@class,"js_btn_save")]')  # 保存 按钮
    _CANCEL_BTN = (By.XPATH, '//div[contains(@class,"ww_operationBar")][1]//a[contains(@class,"js_btn_cancel")]')  # 取消 按钮
    # _TIPS_INFO = (By.XPATH, '//div[contains(@class, "ww_inputWithTips_tips")]//text()')  # 提示信息 - 该账号已被“xinxin”占有
    _TIPS_INFO = (By.CSS_SELECTOR,'.member_edit_item_right.ww_inputWithTips_WithErr div')  # 提示信息 - 该账号已被“xinxin”占有

    @allure.step("输入成员信息，保存成功")
    def add_member_success(self, name, mid, phone):
        self.wait_locate(*self._NAME)  # 等待 姓名输入框 存在
        self.find_ele(*self._NAME).send_keys(name)
        self.find_ele(*self._MID).send_keys(mid)
        self.find_ele(*self._PHONE).send_keys(phone)
        self.find_ele_click(*self._SAVE_BTN)  # 点击保存按钮
        logger.info("添加成员成功，返回通讯录界面")
        from qiyeweixin.pages.contact_page import ContactPage
        return ContactPage(self.driver)

    @allure.step("输入错误成员信息，添加成员失败")
    def add_member_fail(self, name, mid, phone):
        """
        输入重复数据，添加成员失败
        :return: 通讯添加成员页面
        """
        self.wait_locate(*self._NAME)  # 等待 姓名输入框 存在
        self.find_ele(*self._NAME).send_keys(name)
        self.find_ele(*self._MID).send_keys(mid)
        self.find_ele(*self._PHONE).send_keys(phone)
        self.find_ele_click(*self._SAVE_BTN)  # 点击保存按钮
        logger.info("点击保存按钮，停留在添加成员页面")
        return self

    def get_tips_info(self):
        """获取错误提示"""
        tips = self.find_ele(*self._TIPS_INFO).text
        logger.info(f"已获取到提示信息，提示信息为: {tips}")
        return tips

    def click_cancel(self):
        """
        点击取消按钮
        :return: 返回通讯录页面
        """
        self.find_ele_click(*self._CANCEL_BTN)
        from qiyeweixin.pages.contact_page import ContactPage
        return ContactPage(self.driver)
